home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / stupid.c < prev    next >
C/C++ Source or Header  |  1994-06-14  |  16KB  |  519 lines

  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2.    Copyright (C) 1987, 1991, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This file performs stupid register allocation, which is used
  22.    when cc1 gets the -noreg switch (which is when cc does not get -O).
  23.  
  24.    Stupid register allocation goes in place of the the flow_analysis,
  25.    local_alloc and global_alloc passes.  combine_instructions cannot
  26.    be done with stupid allocation because the data flow info that it needs
  27.    is not computed here.
  28.  
  29.    In stupid allocation, the only user-defined variables that can
  30.    go in registers are those declared "register".  They are assumed
  31.    to have a life span equal to their scope.  Other user variables
  32.    are given stack slots in the rtl-generation pass and are not
  33.    represented as pseudo regs.  A compiler-generated temporary
  34.    is assumed to live from its first mention to its last mention.
  35.  
  36.    Since each pseudo-reg's life span is just an interval, it can be
  37.    represented as a pair of numbers, each of which identifies an insn by
  38.    its position in the function (number of insns before it).  The first
  39.    thing done for stupid allocation is to compute such a number for each
  40.    insn.  It is called the suid.  Then the life-interval of each
  41.    pseudo reg is computed.  Then the pseudo regs are ordered by priority
  42.    and assigned hard regs in priority order.  */
  43.  
  44. #include <stdio.h>
  45. #include "config.h"
  46. #include "rtl.h"
  47. #include "hard-reg-set.h"
  48. #include "regs.h"
  49. #include "flags.h"
  50.  
  51. /* Vector mapping INSN_UIDs to suids.
  52.    The suids are like uids but increase monotonically always.
  53.    We use them to see whether a subroutine call came
  54.    between a variable's birth and its death.  */
  55.  
  56. static int *uid_suid;
  57.  
  58. /* Get the suid of an insn.  */
  59.  
  60. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  61.  
  62. /* Record the suid of the last CALL_INSN
  63.    so we can tell whether a pseudo reg crosses any calls.  */
  64.  
  65. static int last_call_suid;
  66.  
  67. /* Element N is suid of insn where life span of pseudo reg N ends.
  68.    Element is  0 if register N has not been seen yet on backward scan.  */
  69.  
  70. static int *reg_where_dead;
  71.  
  72. /* Element N is suid of insn where life span of pseudo reg N begins.  */
  73.  
  74. static int *reg_where_born;
  75.  
  76. /* Numbers of pseudo-regs to be allocated, highest priority first.  */
  77.  
  78. static int *reg_order;
  79.  
  80. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  81.    at the current point in the instruction stream.  */
  82.  
  83. static char *regs_live;
  84.  
  85. /* Indexed by insn's suid, the set of hard regs live after that insn.  */
  86.  
  87. static HARD_REG_SET *after_insn_hard_regs;
  88.  
  89. /* Record that hard reg REGNO is live after insn INSN.  */
  90.  
  91. #define MARK_LIVE_AFTER(INSN,REGNO)  \
  92.   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  93.  
  94. static int stupid_reg_compare    PROTO((int *, int *));
  95. static int stupid_find_reg    PROTO((int, enum reg_class, enum machine_mode,
  96.                        int, int));
  97. static void stupid_mark_refs    PROTO((rtx, rtx));
  98.  
  99. /* Stupid life analysis is for the case where only variables declared
  100.    `register' go in registers.  For this case, we mark all
  101.    pseudo-registers that belong to register variables as
  102.    dying in the last instruction of the function, and all other
  103.    pseudo registers as dying in the last place they are referenced.
  104.    Hard registers are marked as dying in the last reference before
  105.    the end or before each store into them.  */
  106.  
  107. void
  108. stupid_life_analysis (f, nregs, file)
  109.      rtx f;
  110.      int nregs;
  111.      FILE *file;
  112. {
  113.   register int i;
  114.   register rtx last, insn;
  115.   int max_uid, max_suid;
  116.  
  117.   bzero (regs_ever_live, sizeof regs_ever_live);
  118.  
  119.   regs_live = (char *) alloca (nregs);
  120.  
  121.   /* First find the last real insn, and count the number of insns,
  122.      and assign insns their suids.  */
  123.  
  124.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  125.     if (INSN_UID (insn) > i)
  126.       i = INSN_UID (insn);
  127.  
  128.   max_uid = i + 1;
  129.   uid_suid = (int *) alloca ((i + 1) * sizeof (int));
  130.  
  131.   /* Compute the mapping from uids to suids.
  132.      Suids are numbers assigned to insns, like uids,
  133.      except that suids increase monotonically through the code.  */
  134.  
  135.   last = 0;            /* In case of empty function body */
  136.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  137.     {
  138.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  139.     last = insn;
  140.  
  141.       INSN_SUID (insn) = ++i;
  142.     }
  143.  
  144.   last_call_suid = i + 1;
  145.   max_suid = i + 1;
  146.  
  147.   max_regno = nregs;
  148.  
  149.   /* Allocate tables to record info about regs.  */
  150.  
  151.   reg_where_dead = (int *) alloca (nregs * sizeof (int));
  152.   bzero ((char *) reg_where_dead, nregs * sizeof (int));
  153.  
  154.   reg_where_born = (int *) alloca (nregs * sizeof (int));
  155.   bzero ((char *) reg_where_born, nregs * sizeof (int));
  156.  
  157.   reg_order = (int *) alloca (nregs * sizeof (int));
  158.   bzero ((char *) reg_order, nregs * sizeof (int));
  159.  
  160.   reg_renumber = (short *) oballoc (nregs * sizeof (short));
  161.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  162.     reg_renumber[i] = i;
  163.  
  164.   for (i = FIRST_VIRTUAL_REGISTER; i < max_regno; i++)
  165.     reg_renumber[i] = -1;
  166.  
  167.   after_insn_hard_regs
  168.     = (HARD_REG_SET *) alloca (max_suid * sizeof (HARD_REG_SET));
  169.  
  170.   bzero ((char *) after_insn_hard_regs, max_suid * sizeof (HARD_REG_SET));
  171.  
  172.   /* Allocate and zero out many data structures
  173.      that will record the data from lifetime analysis.  */
  174.  
  175.   allocate_for_life_analysis ();
  176.  
  177.   for (i = 0; i < max_regno; i++)
  178.     reg_n_deaths[i] = 1;
  179.  
  180.   bzero (regs_live, nregs);
  181.  
  182.   /* Find where each pseudo register is born and dies,
  183.      by scanning all insns from the end to the start
  184.      and noting all mentions of the registers.
  185.  
  186.      Also find where each hard register is live
  187.      and record that info in after_insn_hard_regs.
  188.      regs_live[I] is 1 if hard reg I is live
  189.      at the current point in the scan.  */
  190.  
  191.   for (insn = last; insn; insn = PREV_INSN (insn))
  192.     {
  193.       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  194.  
  195.       /* Copy the info in regs_live into the element of after_insn_hard_regs
  196.      for the current position in the rtl code.  */
  197.  
  198.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  199.     if (regs_live[i])
  200.       SET_HARD_REG_BIT (*p, i);
  201.  
  202.       /* Update which hard regs are currently live
  203.      and also the birth and death suids of pseudo regs
  204.      based on the pattern of this insn.  */
  205.  
  206.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  207.     stupid_mark_refs (PATTERN (insn), insn);
  208.  
  209.       /* Mark all call-clobbered regs as live after each call insn
  210.      so that a pseudo whose life span includes this insn
  211.      will not go in one of them.
  212.      Then mark those regs as all dead for the continuing scan
  213.      of the insns before the call.  */
  214.  
  215.       if (GET_CODE (insn) == CALL_INSN)
  216.     {
  217.       last_call_suid = INSN_SUID (insn);
  218.       IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  219.                 call_used_reg_set);
  220.  
  221.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  222.         if (call_used_regs[i])
  223.           regs_live[i] = 0;
  224.  
  225.       /* It is important that this be done after processing the insn's
  226.          pattern because we want the function result register to still
  227.          be live if it's also used to pass arguments.  */
  228.       stupid_mark_refs (CALL_INSN_FUNCTION_USAGE (insn), insn);
  229.     }
  230.     }
  231.  
  232.   /* Now decide the order in which to allocate the pseudo registers.  */
  233.  
  234.   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
  235.     reg_order[i] = i;
  236.  
  237.   qsort (®_order[LAST_VIRTUAL_REGISTER + 1],
  238.      max_regno - LAST_VIRTUAL_REGISTER - 1, sizeof (int),
  239.      stupid_reg_compare);
  240.  
  241.   /* Now, in that order, try to find hard registers for those pseudo regs.  */
  242.  
  243.   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
  244.     {
  245.       register int r = reg_order[i];
  246.  
  247.       /* Some regnos disappear from the rtl.  Ignore them to avoid crash.  */
  248.       if (regno_reg_rtx[r] == 0)
  249.     continue;
  250.  
  251.       /* Now find the best hard-register class for this pseudo register */
  252.       if (N_REG_CLASSES > 1)
  253.     reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r], 
  254.                        reg_preferred_class (r),
  255.                        PSEUDO_REGNO_MODE (r),
  256.                        reg_where_born[r],
  257.                        reg_where_dead[r]);
  258.  
  259.       /* If no reg available in that class, try alternate class.  */
  260.       if (reg_renumber[r] == -1 && reg_alternate_class (r) != NO_REGS)
  261.     reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r],
  262.                        reg_alternate_class (r),
  263.                        PSEUDO_REGNO_MODE (r),
  264.                        reg_where_born[r],
  265.                        reg_where_dead[r]);
  266.     }
  267.  
  268.   if (file)
  269.     dump_flow_info (file);
  270. }
  271.  
  272. /* Comparison function for qsort.
  273.    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
  274.  
  275. static int
  276. stupid_reg_compare (r1p, r2p)
  277.      int *r1p, *r2p;
  278. {
  279.   register int r1 = *r1p, r2 = *r2p;
  280.   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  281.   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  282.   int tem;
  283.  
  284.   tem = len2 - len1;
  285.   if (tem != 0)
  286.     return tem;
  287.  
  288.   tem = reg_n_refs[r1] - reg_n_refs[r2];
  289.   if (tem != 0)
  290.     return tem;
  291.  
  292.   /* If regs are equally good, sort by regno,
  293.      so that the results of qsort leave nothing to chance.  */
  294.   return r1 - r2;
  295. }
  296.  
  297. /* Find a block of SIZE words of hard registers in reg_class CLASS
  298.    that can hold a value of machine-mode MODE
  299.      (but actually we test only the first of the block for holding MODE)
  300.    currently free from after insn whose suid is BIRTH
  301.    through the insn whose suid is DEATH,
  302.    and return the number of the first of them.
  303.    Return -1 if such a block cannot be found.
  304.  
  305.    If CALL_PRESERVED is nonzero, insist on registers preserved
  306.    over subroutine calls, and return -1 if cannot find such.  */
  307.  
  308. static int
  309. stupid_find_reg (call_preserved, class, mode, born_insn, dead_insn)
  310.      int call_preserved;
  311.      enum reg_class class;
  312.      enum machine_mode mode;
  313.      int born_insn, dead_insn;
  314. {
  315.   register int i, ins;
  316. #ifdef HARD_REG_SET
  317.   register        /* Declare them register if they are scalars.  */
  318. #endif
  319.     HARD_REG_SET used, this_reg;
  320. #ifdef ELIMINABLE_REGS
  321.   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
  322. #endif
  323.  
  324.   COPY_HARD_REG_SET (used,
  325.              call_preserved ? call_used_reg_set : fixed_reg_set);
  326.  
  327. #ifdef ELIMINABLE_REGS
  328.   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
  329.     SET_HARD_REG_BIT (used, eliminables[i].from);
  330. #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
  331.   SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
  332. #endif
  333. #else
  334.   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  335. #endif
  336.  
  337.   for (ins = born_insn; ins < dead_insn; ins++)
  338.     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  339.  
  340.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  341.  
  342.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  343.     {
  344. #ifdef REG_ALLOC_ORDER
  345.       int regno = reg_alloc_order[i];
  346. #else
  347.       int regno = i;
  348. #endif
  349.  
  350.       /* If a register has screwy overlap problems,
  351.      don't use it at all if not optimizing.
  352.      Actually this is only for the 387 stack register,
  353.      and it's because subsequent code won't work.  */
  354. #ifdef OVERLAPPING_REGNO_P
  355.       if (OVERLAPPING_REGNO_P (regno))
  356.     continue;
  357. #endif
  358.  
  359.       if (! TEST_HARD_REG_BIT (used, regno)
  360.       && HARD_REGNO_MODE_OK (regno, mode))
  361.     {
  362.       register int j;
  363.       register int size1 = HARD_REGNO_NREGS (regno, mode);
  364.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
  365.       if (j == size1)
  366.         {
  367.           CLEAR_HARD_REG_SET (this_reg);
  368.           while (--j >= 0)
  369.         SET_HARD_REG_BIT (this_reg, regno + j);
  370.           for (ins = born_insn; ins < dead_insn; ins++)
  371.         {
  372.           IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  373.         }
  374.           return regno;
  375.         }
  376. #ifndef REG_ALLOC_ORDER
  377.       i += j;        /* Skip starting points we know will lose */
  378. #endif
  379.     }
  380.     }
  381.  
  382.   return -1;
  383. }
  384.  
  385. /* Walk X, noting all assignments and references to registers
  386.    and recording what they imply about life spans.
  387.    INSN is the current insn, supplied so we can find its suid.  */
  388.  
  389. static void
  390. stupid_mark_refs (x, insn)
  391.      rtx x, insn;
  392. {
  393.   register RTX_CODE code;
  394.   register char *fmt;
  395.   register int regno, i;
  396.  
  397.   if (x == 0)
  398.     return;
  399.  
  400.   code = GET_CODE (x);
  401.  
  402.   if (code == SET || code == CLOBBER)
  403.     {
  404.       if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  405.     {
  406.       /* Register is being assigned.  */
  407.       regno = REGNO (SET_DEST (x));
  408.  
  409.       /* For hard regs, update the where-live info.  */
  410.       if (regno < FIRST_PSEUDO_REGISTER)
  411.         {
  412.           register int j
  413.         = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  414.  
  415.           while (--j >= 0)
  416.         {
  417.           regs_ever_live[regno+j] = 1;
  418.           regs_live[regno+j] = 0;
  419.  
  420.           /* The following line is for unused outputs;
  421.              they do get stored even though never used again.  */
  422.           MARK_LIVE_AFTER (insn, regno);
  423.  
  424.           /* When a hard reg is clobbered, mark it in use
  425.              just before this insn, so it is live all through.  */
  426.           if (code == CLOBBER && INSN_SUID (insn) > 0)
  427.             SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (insn) - 1],
  428.                       regno);
  429.         }
  430.         }
  431.       /* For pseudo regs, record where born, where dead, number of
  432.          times used, and whether live across a call.  */
  433.       else
  434.         {
  435.           /* Update the life-interval bounds of this pseudo reg.  */
  436.  
  437.           /* When a pseudo-reg is CLOBBERed, it is born just before
  438.          the clobbering insn.  When setting, just after.  */
  439.           int where_born = INSN_SUID (insn) - (code == CLOBBER);
  440.  
  441.           reg_where_born[regno] = where_born;
  442.  
  443.           /* The reg must live at least one insn even
  444.          in it is never again used--because it has to go
  445.          in SOME hard reg.  Mark it as dying after the current
  446.          insn so that it will conflict with any other outputs of
  447.          this insn.  */
  448.           if (reg_where_dead[regno] < where_born + 2)
  449.         {
  450.           reg_where_dead[regno] = where_born + 2;
  451.           regs_live[regno] = 1;
  452.         }
  453.  
  454.           /* Count the refs of this reg.  */
  455.           reg_n_refs[regno]++;
  456.  
  457.           if (last_call_suid < reg_where_dead[regno])
  458.         reg_n_calls_crossed[regno] += 1;
  459.         }
  460.     }
  461.  
  462.       /* Record references from the value being set,
  463.      or from addresses in the place being set if that's not a reg.
  464.      If setting a SUBREG, we treat the entire reg as *used*.  */
  465.       if (code == SET)
  466.     {
  467.       stupid_mark_refs (SET_SRC (x), insn);
  468.       if (GET_CODE (SET_DEST (x)) != REG)
  469.         stupid_mark_refs (SET_DEST (x), insn);
  470.     }
  471.       return;
  472.     }
  473.  
  474.   /* Register value being used, not set.  */
  475.  
  476.   if (code == REG)
  477.     {
  478.       regno = REGNO (x);
  479.       if (regno < FIRST_PSEUDO_REGISTER)
  480.     {
  481.       /* Hard reg: mark it live for continuing scan of previous insns.  */
  482.       register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  483.       while (--j >= 0)
  484.         {
  485.           regs_ever_live[regno+j] = 1;
  486.           regs_live[regno+j] = 1;
  487.         }
  488.     }
  489.       else
  490.     {
  491.       /* Pseudo reg: record first use, last use and number of uses.  */
  492.  
  493.       reg_where_born[regno] = INSN_SUID (insn);
  494.       reg_n_refs[regno]++;
  495.       if (regs_live[regno] == 0)
  496.         {
  497.           regs_live[regno] = 1;
  498.           reg_where_dead[regno] = INSN_SUID (insn);
  499.         }
  500.     }
  501.       return;
  502.     }
  503.  
  504.   /* Recursive scan of all other rtx's.  */
  505.  
  506.   fmt = GET_RTX_FORMAT (code);
  507.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  508.     {
  509.       if (fmt[i] == 'e')
  510.     stupid_mark_refs (XEXP (x, i), insn);
  511.       if (fmt[i] == 'E')
  512.     {
  513.       register int j;
  514.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  515.         stupid_mark_refs (XVECEXP (x, i, j), insn);
  516.     }
  517.     }
  518. }
  519.